package com.amado.course42Socket.tcp.homework;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class HelloDialogueClient {
    public static void main(String[] args) throws Exception {
        String host = "localhost";
        int port = 9999;
        Socket socket = new Socket(host, port);
        //开启一个线程去读取对方的聊天的信息
        new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					BufferedReader  lineNumberReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			        while(true){
			        	System.out.println(lineNumberReader.readLine());
			        }
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}).start();
        PrintWriter printwriter = new PrintWriter(socket.getOutputStream(),true);
		printwriter.print("你已经连接到服务器");
		Scanner scanner = new Scanner(System.in);
		while(true){
			//主线程负责我的聊天信息输入
			printwriter.println(scanner.nextLine());
		}
    }
}